package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author dxx
 * @since 2022-09-19
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor author) {

        if (author == null || author.getUserId() == null || StringUtils.isEmpty(author.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 参数校验todo
        // 判断是否重复  这里根据user_id 来查询
        LambdaQueryWrapper<ApAuthor> query = new LambdaQueryWrapper<>();
        query.eq(ApAuthor::getUserId, author.getUserId());
        ApAuthor one = this.getOne(query);
        // 模拟异常
        // int i = 1 / 0;
        if (one == null) {
            author.setCreatedTime(new Date());
            this.save(author);
            return ResponseResult.okResult(author);
        }
        return ResponseResult.okResult(one);
    }
}
